package com.shujia.sql

import org.apache.spark.sql.expressions.UserDefinedFunction
import org.apache.spark.sql.{DataFrame, SparkSession}

object Demo11UDF {
  def main(args: Array[String]): Unit = {
    val sparkSession: SparkSession = SparkSession.builder()
      .master("local")
      .appName("udf函数演示")
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()
    import sparkSession.implicits._
    import org.apache.spark.sql.functions._
    /**
     * 1、在使用DSL的时候使用自定义函数
     */
    val studentsDF: DataFrame = sparkSession.read
      .format("csv")
      .option("sep", ",")
      .schema("id STRING,name STRING,age INT,gender STRING,clazz STRING")
      .load("spark/data/students.csv")

    //    studentsDF.show()

    //编写自定义函数
    //udf中编写的是scala代码
    val shujia_fun1: UserDefinedFunction = udf((str: String) => "数加：" + str)

//    studentsDF.select(shujia_fun1($"clazz")).show()

    /**
     * 1、使用SQL语句中使用自定函数
     */
    studentsDF.createOrReplaceTempView("students")
    //将自定义的函数变量注册成一个函数
    sparkSession.udf.register("shujia_str",shujia_fun1)
    sparkSession.sql(
      """
        |select clazz,shujia_str(clazz) as new_clazz from students
        |""".stripMargin).show()


  }
}
